package edu.cs.usask.ca.silver.plugin.rcp.handlers.create;

import org.apache.log4j.Logger;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.window.Window;

import edu.cs.usask.ca.silver.businesslogic.ids.UserID;
import edu.cs.usask.ca.silver.plugin.rcp.Activator;
import edu.cs.usask.ca.silver.plugin.rcp.dao.SilverBusinessLogicController;
import edu.cs.usask.ca.silver.plugin.rcp.dialogs.create.CreateUserDialog;
import edu.cs.usask.ca.silver.plugin.rcp.jobs.LoggerReportScheduler;

/**
 * This dialog asks necessary information from user to register a new user. If
 * the "projectview" parameter is given to the command, it will register a user
 * to associated database. If the "projectview" parameter is not given (event is
 * null), it will register the user to local database.
 * 
 * 
 * 
 * @author Yudi Xue
 * 
 */
public class CreateUserHandler extends AbstractHandler {
	private static Logger logger = Logger.getLogger(CreateUserHandler.class);

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		CreateUserDialog dialog = null;
		dialog = new CreateUserDialog(Activator.getDefault().getWorkbench()
				.getActiveWorkbenchWindow().getShell());

		dialog.create();

		UserID id = null;
		if (dialog.open() == Window.OK) {
			try {
				// Overwrite user information
				SilverBusinessLogicController.storeUserLoginInformation(
						dialog.getFirstName(), dialog.getLastName(),
						dialog.getEmail(), dialog.getPassWord());
				logger.debug("creating user" + dialog.getFirstName() + dialog.getLastName() + dialog.getEmail());
			} catch (Exception e) {
				LoggerReportScheduler.scheduleErrorLogging(logger,"error while creating user in rcp command handler", e);
			}
		}
		return id;
	}

}